﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// Added lib's
using InterIMAP;
using System.Collections;
using System.Threading;

namespace IMAPExperiment1
{
    class Program
    {
        static void Main(string[] args)
        {
            bool bNotExit = true;
            IMAPClient client = null;
            IMAPFolder f = null;
            IMAPConfig config = null;
            IMAPSearchQuery query = null;
            IMAPSearchResult result = null;
            IMAPMessage msg = null;
            string sHost = "";
            string sUser = "";
            string sPwd = "";
            string sSSL = "";
            bool bSSL = true;
            string sCacheFormat = "";
            string sCacheFile = "";
            bool bUseCache = true;
            bool bAutoLog = true;
            string sAutoLog = "";


            while (bNotExit)
            {
                try
                {
                    Console.WriteLine("Select the action");
                    Console.WriteLine("0    Config");
                    Console.WriteLine("1    Login");
                    Console.WriteLine("2    Select/Examine");
                    Console.WriteLine("3    Search");
                    Console.WriteLine("4    Debug");
                    Console.WriteLine("5    GetQuota");
                    Console.WriteLine("6    Logout");
                    Console.WriteLine("7    SaveFile");
                    Console.WriteLine("9    Exit");
                    Console.Write("Input  :[9] ");
                    string sInput = Console.ReadLine();
                    int input = 0;
                    try
                    {
                        input = Convert.ToInt32(sInput, 10);
                    }
                    catch (System.FormatException e)
                    {
                        Console.WriteLine("Error:{0}:{1}", e.Message, e.InnerException);
                    }
                    switch (input)
                    {
                        case 0:
                            {
                                Console.WriteLine("Choose IMAP account:");
                                Console.WriteLine("0     Gmail");
                                Console.WriteLine("1     Aim");
                                // Console.WriteLine("2     not defined");
                                // Console.WriteLine("3     not defined");
                                // Console.WriteLine("4     not defined");
                                Console.WriteLine("5     User define");
                                Console.Write("Account  :[5] ");
                                string sInputConfig = Console.ReadLine();
                                int iInputConfig = 0;
                                try
                                {
                                    iInputConfig = Convert.ToInt32(sInputConfig, 10);
                                }
                                catch (System.FormatException e)
                                {
                                    Console.WriteLine("Error:{0}:{1}", e.Message, e.InnerException);
                                }
                                switch (iInputConfig)
                                {
                                    case 0:
                                        {
                                            sHost = "imap.gmail.com";
                                            sUser = "guyroee";
                                            sPwd = "infini2008";
                                            bSSL = true;
                                            sCacheFormat = "Xml";
                                            sCacheFile = "C:\\temp\\gmailCache.xml";
                                            bUseCache = true;
                                        }
                                        break;
                                    case 1:
                                        {
                                            sHost = "imap.aim.com";
                                            sUser = "roeeguy";
                                            sPwd = "infini2008";
                                            bSSL = false;
                                            sCacheFormat = "Xml";
                                            sCacheFile = "aimCache";
                                            bUseCache = true;
                                        }
                                        break;
                                    case 2:
                                        {

                                        }
                                        break;
                                    case 5:
                                        {
                                            Console.Write("Host[]: ");
                                            sHost = Console.ReadLine();
                                            if (sHost.Length < 1)
                                                sHost = "imap.gmail.com";
                                            Console.Write("User[]: ");
                                            sUser = Console.ReadLine();
                                            if (sUser.Length < 1)
                                                sUser = "guyroee";
                                            Console.Write("Password []: ");
                                            sPwd = Console.ReadLine();
                                            if (sPwd.Length < 1)
                                                sPwd = "infini2008";
                                            Console.Write("Use SSL [Y/N] (Y): ");
                                            sSSL = Console.ReadLine();
                                            if (sSSL.Length < 1)
                                            {
                                                sSSL = "Y";
                                            }
                                            bSSL = ((sSSL.CompareTo("Y") == 0) || (sSSL.CompareTo("y") == 0)) ? true : false;
                                        }
                                        break;
                                }
                                Console.Write("AutoLogon [Y/N] (Y): ");
                                sAutoLog = Console.ReadLine();
                                bAutoLog = (sAutoLog.CompareTo("Y") == 0 || sAutoLog.CompareTo("y") == 0) ? true : false;
                                if (sAutoLog.Length < 1)
                                    bAutoLog = true;
                                Console.Write("Use cache file [Y / N] (Y): ");
                                string sUseCache = Console.ReadLine();
                                bUseCache = (sUseCache.CompareTo("Y") == 0 || sUseCache.CompareTo("y") == 0) ? true : false;
                                if (bUseCache)
                                {
                                    Console.Write("Cache File []: ");
                                    sCacheFile = Console.ReadLine();
                                    if (sCacheFile.Length < 1)
                                        sCacheFile = "cache";
                                    Console.Write("Cache Format [Xml / Binary]: (Xml) ");
                                    sCacheFormat = Console.ReadLine();
                                    if (sCacheFormat.Length < 1)
                                        sCacheFormat = "Xml";
                                }
                                config = new IMAPConfig(sHost, sUser, sPwd, bSSL, bAutoLog, "INBOX");
                                if (bUseCache)
                                {
                                    config.CacheFile = "c:\\temp\\cache.xml";
                                    config.Format = sCacheFormat.CompareTo("Xml") == 0 ? CacheFormat.XML : CacheFormat.Binary;
                                }
                                else
                                {
                                    config.CacheFile = "";
                                }
                                config.SaveConfig("settings.cfg");
                                // IMAPConfig config = new IMAPConfig(@"c:\test1.cfg");
                                client = new IMAPClient(config, null, 5);
                            }
                            break;
                        case 1:
                            {
                                Console.WriteLine("########################################");
                                Console.WriteLine("Host: {0}", config.Host);
                                Console.WriteLine("User: {0}", config.UserName);
                                Console.WriteLine("SSL: {0}", config.UseSSL);
                                Console.WriteLine("AutoLogon: {0}", config.AutoLogon);
                                Console.WriteLine("########################################");
                                client.Logon();
                            }
                            break;

                        case 2:
                            {
                                Console.Write("Folder: [INBOX] ");
                                string sFolder = Console.ReadLine();
                                Console.Write("Select:s Examine:e [s] ");
                                string sChoice = Console.ReadLine();
                                if (sFolder.Length < 1)
                                    sFolder = "INBOX";
                                f = client.GetSingleFolder(sFolder);
                                if (sChoice == "e")
                                {
                                    f.Examine();
                                }
                                else
                                {
                                    f.Select();
                                }
                            }
                            break;
                        case 3:
                            {
                                query = new IMAPSearchQuery();
                                Console.Write("Search String:");
                                string sSearchCrit = Console.ReadLine();
                                query = IMAPSearchQuery.QuickSearchSubject(sSearchCrit);
                                result = f.Search(query);
                                if (result.Messages.Capacity > 0)
                                {
                                    msg = result.Messages[0];
                                    Console.WriteLine("Subject: " + msg.Subject);
                                }
                            }
                            break;
                        case 4: // FetchHeader
                            {
                            }
                            break;
                        case 5: // Check Quota 
                            {
                                bool bUnlimitedQuota = false;
                                int nUsedKBytes = 0;
                                int nTotalKBytes = 0;
                                nUsedKBytes = f.Quota.CurrentSize;
                                nTotalKBytes = f.Quota.MaxSize;
                                Console.WriteLine("UsedKBytes: {1}, TotalKBytes: {2}", nUsedKBytes, nTotalKBytes);
                                Console.WriteLine("Quota: ", f.Quota.ToString());
                            }
                            break;
                        case 6: // Logoff
                            client.Logoff();
                            break;
                        case 7: // Save File 
                            msg.RefreshData(true, true);
                            foreach (IMAPFileAttachment attachment in msg.Attachments)
                            {
                                attachment.SaveFile("C:\\temp\\");
                            }
                            break;
                        case 9: // Exit
                            bNotExit = false;
                            break;
                    }
                }
                //catch (ImapException e)
                //{
                //    Console.WriteLine("Error:{0}:{1}", e.Message, e.InnerException);
                //}
                catch (System.FormatException e)
                {
                    Console.WriteLine("Error:{0}:{1}", e.Message, e.InnerException);
                }
            }

        }
    }
}
